<template>
	<view class="container" style="display: flex;flex-direction: column;">
		<view class="top" style="width: 100%;height: 76rpx;background-color: #FFF;">
			<u-search placeholder="请输入商品名称" v-model="itemName" @search='search' @custom='search'></u-search>
		</view>
		<!-- 占位 -->
		<view style="width: 750rpx;height: 76rpx;flex-shrink: 0;z-index: -1;"></view>
		<div style="display: flex;flex-direction: row;width: 100%;background-color: #EFEFEF;" class="content">
			<scroll-view class="left" scroll-y="true"
				style="position: fixed;width: 180rpx;height:100vh;background-color: #EFEFEF;display: flex;flex-direction: column;align-items: center;flex-shrink: 0;overflow-y: auto;"
				:style="{
				'width': nodeModel.configDict.componentStyle.marginLeft+'rpx'
			}">
				<block v-if="nodeModel.configDict.componentStyle.isShowForBtn1">
					<view v-if="nodeModel.configDict.componentStyle.options1" style="display: flex;flex-direction: row;align-items: center;height: 50px;line-height: 50px;">
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
						<view style="margin: 0 5px;color: #666666;font-size: 14px;">
							{{nodeModel.configDict.componentStyle.options1}}</view>
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
					</view>
					<!-- 组一 -->
					<view v-if="nodeModel.configDict.componentStyle.type=='1'"
						style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup1" :key='index' @click="menuAction(item)"
							style="width: 100%;line-height: 40rpx;text-align: center;padding: 20rpx 0;display: flex;flex-direction: row;align-items: center;"
							:style="{
					                'background-color': item.isShow?'#FFF':'',
					              }">
							<view style="width: 4rpx;height: 60rpx;" :style="{
						'background-color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'',
					  }"></view>
							<view style="width: 100%;text-align: center;margin-left: 4rpx;" :style="{
						'color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'',
					  }">
								{{item.title}}
							</view>
						</view>
					</view>
					<view v-if="nodeModel.configDict.componentStyle.type=='2'"
						style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup1" :key='index' @click="menuAction(item)"
							style="width: 100%;line-height: 20px;text-align: center;font-size: 15px;padding: 20rpx 0;"
							:style="{
						'color': item.isShow?nodeModel.configDict.componentStyle.colorForBtn1:'#666666',
						'background-color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'#EFEFEF',
					}">{{item.title}}</view>
					</view>
				</block>
				<block v-if="nodeModel.configDict.componentStyle.isShowForBtn2">
					<view v-if="nodeModel.configDict.componentStyle.options2" style="display: flex;flex-direction: row;align-items: center;height: 50px;line-height: 50px;">
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
						<view style="margin: 0 5px;color: #666666;font-size: 14px;">
							{{nodeModel.configDict.componentStyle.options2}}</view>
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
					</view>
					<!-- 组二 -->
					<view v-if="nodeModel.configDict.componentStyle.type=='1'"
						style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup2" :key='index' @click="menuAction(item)"
							style="width: 100%;line-height: 40rpx;text-align: center;padding: 20rpx 0;display: flex;flex-direction: row;align-items: center;"
							:style="{
					                'background-color': item.isShow?'#FFF':'',
					              }">
							<view style="width: 4rpx;height: 30rpx;" :style="{
						'background-color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'',
					  }"></view>
							<view style="width: 100%;text-align: center;margin-left: 4rpx;" :style="{
						'color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'',
					  }">
								{{item.title}}
							</view>
						</view>
					</view>
					<view v-if="nodeModel.configDict.componentStyle.type=='2'"
						style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup2" :key='index' @click="menuAction(item)"
							style="width: 100%;line-height: 20px;text-align: center;font-size: 15px;padding: 20rpx 0;"
							:style="{
						'color': item.isShow?nodeModel.configDict.componentStyle.colorForBtn2:'#666666',
						'background-color': item.isShow?nodeModel.configDict.componentStyle.mainColor:'#EFEFEF',
					}">{{item.title}}</view>
					</view>
				</block>
			</scroll-view>

			<view
				style="background-color: #f8f8f8;"
				class="content" :style="{
				'marginLeft': nodeModel.configDict.componentStyle.marginLeft+'rpx'
			}">
				<view class="">
					<!-- <scroll-view scroll-x="true">
						<div style="display: flex;flex-direction: row;align-items: center;">
							<div>撒递</div>
							<div>撒递</div>
							<div>撒递</div>
							<div>撒递</div>
						</div>
					</scroll-view> -->
					<div style="display: flex;flex-direction: row;flex-wrap: wrap;align-content: flex-start;padding-bottom: 200rpx;">
						<view v-for="(item, index) in itemList" :key='index' @click="itemHandle(item)"
							style="display: flex;flex-direction: row;margin-top: 12rpx;background-color: #FFF;width: 540rpx;margin-left: 8rpx;">
							<image :src="item.coverUrl" mode='aspectFill' style="width: 180rpx;height: 180rpx;padding: 16rpx;border-radius: 4rpx;"></image>
							<view style="padding: 16rpx;padding-left: 0;">
								<view style="font-size: 28rpx;" class="limitLineNumOfTwo">{{item.name}}</view>
								<view style="margin-top: 10rpx;color: #fa3534;" v-if="item.activityKey=='incomesDeduction'">
									<text
										v-if="item.suggestSellingPrice>0">¥{{ $count.mul(item.suggestSellingPrice||0, 1) }}</text>
									<text v-if="item.suggestSellingPrice>0">+</text>
									<text>{{ $count.mul(item.suggestIncomesPrice||0, 1) }}积分</text>
								</view>
								<view v-else style="margin-top: 10rpx;color: #fa3534;">
									¥{{$count.mul(item.priceModel.sellingPrice||0, 1)}}</view>
							</view>
						</view>
						<view v-if='page==0' style="color: #999;margin-top: 40rpx;text-align: center;font-size: 24rpx;width: 540rpx;">没有更多商品了</view>
					</div>
				</view>
				
			</view>
		</div>
		<jcTabbarItem :current="currentTabbarIndex" :list='dynamicTabbar'></jcTabbarItem>
	</view>
</template>

<script>
	import {
		page
	} from '@/api/page.js'
	import {
		common
	} from "@/api/common.js";
	import {
		merchant
	} from '@/api/merchant.js'
	import jcTabbarItem from '@/components/jc-tabbarItem/index.vue'
	export default {
		components: {
			jcTabbarItem
		},
		data() {
			return {
				type: '',
				page: 1,
				size: 20,
				dynamicTabbar: [],
				currentTabbarIndex: 0,
				titleForGroup1: [],
				titleForGroup2: [],
				lastItem: {},
				itemList: [],
				nodeList: [],
				nodeModel: {},
				pageConfig: {},
				selectAdvert: [{}],
				itemName: ''
			}
		},
		onLoad() {
			// this.findShopinCateList()
			// this.findActivityList()
			uni.showShareMenu({
				withShareTicket:true,
				//设置下方的Menus菜单，才能够让发送给朋友与分享到朋友圈两个按钮可以点击
				menus:["shareAppMessage","shareTimeline"]
			
			})
			this.getPageByClientname()
		},
		onShow() {
			this.dynamicTabbar = this.$store.getters.getDynamicTabbar
			var tabbars = this.dynamicTabbar.filter(item => item.key == this.$Route.name)
			this.currentTabbarIndex = tabbars.length >= 1 ? tabbars[0].sort : '0'

			var item = this.lastItem
			if (this.nodeModel.configDict && this.nodeModel.configDict.componentStyle.isShowForBtn1 && this.titleForGroup1
				.length > 0) {
				this.menuAction(item)
			} else if (this.nodeModel.configDict && this.nodeModel.configDict.componentStyle.isShowForBtn2 && this
				.titleForGroup2.length > 0) {
				this.menuAction(item)
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getItemList()
		},
		onReachBottom() {
			console.log('上拉加载更多')
			if (this.page == 0) {
				return
			}
			this.getItemList()
		},
		onShareAppMessage(res) {
			return {
				title: this.$store.getters.clientInfo.appname,
				path: 'pages/home/home',
				imageUrl: this.$store.getters.clientInfo.shareCoverUrl,
			}
		},
		methods: {
			getPageByClientname() {
				page.getPageByClientname({
					name: this.$Route.name
				}).then(res => {
					uni.stopPullDownRefresh()
					if (res.data.code == 10000) {
						this.pageConfig = res.data.data
						this.setPageConfig(this.pageConfig)
						this.nodeList = res.data.data.nodeList
						for (var node of this.nodeList) {
							if (node.componentModel.name == 'itemCate') {
								this.nodeModel = node
								for (var item of this.nodeModel.configDict.itemList) {
									item.isShow = false
									if (item.types == 'group1') {
										this.titleForGroup1.push(item)
									} else if (item.types == 'group2') {
										this.titleForGroup2.push(item)
									}
								}
								if (this.nodeModel.configDict.componentStyle.isShowForBtn1 && this.titleForGroup1
									.length > 0) {
									this.menuAction(this.titleForGroup1[0])
								} else if (this.nodeModel.configDict.componentStyle.isShowForBtn2 && this
									.titleForGroup2.length > 0) {
									this.menuAction(this.titleForGroup2[0])
								}
							}
						}
					}
				})
			},
			setPageConfig(pageConfig) {
				this.pageConfig = pageConfig
				uni.setNavigationBarTitle({
					title: this.pageConfig.title
				})
			},
			menuAction(item) {
				console.log('点击菜单', item)
				this.lastItem.isShow = false
				item.isShow = true
				this.lastItem = item


				this.page = 1
				this.getItemList()
			},
			search() {
				this.page = 1
				this.getItemList()
			},
			getItemList() {
				var query = ''
				if (this.lastItem.query) {
					query = JSON.parse(this.lastItem.query)
				}
				var params = {
					page: this.page,
					size: this.size,
					name: this.itemName,
				}
				if (!this.lastItem.query) return //如果删选条件为空，不请求数据
				params = Object.assign(query, params) //当前页面展示的列表
				merchant.getItemListForC(params).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if (this.page == 1) {
							this.itemList = []
						}
						var list = res.data.data.list
						this.itemList = [...this.itemList, ...list]
						if (res.data.data.next) {
							this.page += 1
						} else {
							this.page = 0
						}
					} else {
						this.itemList = []
					}
				})
			},
			itemHandle(item) {
				this.$Router.push({
					name: 'itemDetail',
					params: {
						obj_id: item.id,
						type: 'item'
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		height: 100%;
	}

	.top {
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
	}

	.content {
		height: calc(100vh - 78rpx);
	}
</style>
